﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_BLL;
using ANZ_AIM_FRAMEWORK;
using ANZ_AIM_BLL.Model;
using ANZ_AIM_GUI.Utils;
using System.Text.RegularExpressions;

namespace ANZ_AIM_GUI
{
    public partial class frmSaleSQMUpload : Form
    {
        public List<SaleSQM> saleSQMList = new List<SaleSQM>();
        public frmSaleSQMUpload()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ExcelDialog = new OpenFileDialog();
            ExcelDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
            ExcelDialog.InitialDirectory = @"C:\";
            ExcelDialog.Title = "Select your file excel";
            if (ExcelDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtFileName.Text = ExcelDialog.FileName;
                txtFileName.ReadOnly = true;
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {

            try
            {
                if (string.IsNullOrEmpty(txtFileName.Text.Trim()))
                {
                    MessageBox.Show("File Name cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtFileName.Focus();
                    return;
                }

                //insert BatchFileSaleInfo DB
                int IDFileBatchUpload = 0;
                BatchFileSaleInfoUploadBLL batchFileSaleInfoBll = new BatchFileSaleInfoUploadBLL();
                string[] s = txtFileName.Text.Trim().Split('\\');
                batchFileSaleInfoBll.Save(new BatchFileSaleInfoUpload() { CreatedDate = MiscHelper.DateTimeToShort(DateTime.Now), FileName = s[s.Length - 1], Status = Constant.NEED_TO_VERIFY,Type=Constant.TYPE_SQM }, ref IDFileBatchUpload);


                ////bind datarow to list 
                DataTable dtData = NPOIHelper.ImportExceltoDt(txtFileName.Text.Trim(), 0, 0);

                

                foreach (DataRow dr in dtData.Rows)
                {
                    PackObject(dr, IDFileBatchUpload.ToString());
                }

                ////insert SQMTemp
                SaleSQMBLL saleSQMBll = new SaleSQMBLL();
                saleSQMBll.SaveSQM(saleSQMList);

                //bind datagridview
                DataTable dt = new DataTable();
                dt.Columns.Add("Batch File Change");
                dt.Columns.Add("File Name");
                dt.Columns.Add("Uploaded Date");

                DataRow datarow = dt.NewRow();
                datarow["Batch File Change"] = IDFileBatchUpload;
                datarow["File Name"] = s[s.Length - 1];
                datarow["Uploaded Date"] = DateTime.Now;
                dt.Rows.Add(datarow);

                dgvData.DataSource = null;
                dgvData.Columns.Clear();
                dgvData.DataSource = dt;
                dgvData.AutoResizeColumns();

                MessageBox.Show("Upload Successfully.", Constant.SYS_INFORMATION, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {

            }
        }

        private void PackObject(DataRow drData, string batchFileChangeID)
        {
            SaleSQM saleSQM = new SaleSQM();
            saleSQM.SaleCode = drData["Sales Code"].ToString();
            saleSQM.Channel = drData["Channel"].ToString();
            saleSQM.SaleName = drData["Sales Name"].ToString();
            saleSQM.Role = drData["Role"].ToString();
            saleSQM.GrandTotalRiskPoint = drData["Grand Total Risk Point"].ToString();
            saleSQM.IncentiveClawBack = drData["Incentive claw back"].ToString();
            saleSQM.ComplianceGateKeeper = drData["Compliance gate keeper"].ToString();
            saleSQM.DisciplinaryImplications = drData["Disciplinary Implications"].ToString();
            saleSQM.Month = drData["Month"].ToString();
            

            saleSQMList.Add(saleSQM);
        }

    }
}
